package com.cndw.kungfu.model.activity.holiday;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.HolidayCommSerialize;
/**
 * 重阳风筝活动数据
 * 
 * @author Everest
 *
 */
public class KiteData implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1312704250517549147L;
	/**
	 * 风筝总积分集合
	 */
	private Map<Long, Integer> kiteMap = new ConcurrentHashMap<Long, Integer>();
	/**
	 * 兑换后积分集合
	 */
	private Map<Long, Integer> leaveScore = new ConcurrentHashMap<Long, Integer>();
	
	private transient List<Object[]> kiteRanking = new ArrayList<Object[]>();
	
	private static int lastRankTime = 0;
	
	private static int endTime = 0;
	
	static {
		endTime = TimeProcesser.getUnixTimeSilent("2012-10-30 07:00:00");
	}
	
	public static KiteData getInstance() {
		return HolidayCommSerialize.getInstance().getKiteData();
	}
	
	public void alterKiteMap(long playerId, int n) {
		if (kiteMap.containsKey(playerId)) {
			kiteMap.put(playerId, kiteMap.get(playerId) + n);
		} else {
			kiteMap.put(playerId, n);
		}
	}
	
	public int getKite(long playerId) {
		if (kiteMap.containsKey(playerId)) {
			return kiteMap.get(playerId);
		}
		return 0;
	}
	
	public void alterLeaveScore(long playerId, int n) {
		if (leaveScore.containsKey(playerId)) {
			leaveScore.put(playerId, leaveScore.get(playerId) + n);
		} else {
			leaveScore.put(playerId, n);
		}
	}
	
	public int getLeaveScore(long playerId) {
		if (leaveScore.containsKey(playerId)) {
			return leaveScore.get(playerId);
		}
		return 0;
	}
	
	public List<Object[]> getKiteRanking() {
		if (TimeProcesser.getUnixTime() - lastRankTime > 300) {
			synchronized (this) {
				if (TimeProcesser.getUnixTime() - lastRankTime > 300) {
					kiteRank();
				}
			}
		}
		return kiteRanking;
	}
	
	private void kiteRank() {
		if (kiteRanking == null) {
			kiteRanking = new ArrayList<Object[]>();
		} else {
			kiteRanking.clear();
		}
		
		if (kiteMap.isEmpty()) {
			return;
		}
		
		List<Entry<Long, Integer>> list = getRankList(kiteMap);
		int orderId = 0;
		for (Entry<Long, Integer> e : list) {
			Player player = Context.getPlayerServer().getByPlayerId(e.getKey());
			if (player == null) {
				continue;
			}
			orderId ++;
			Object[] obj = new Object[4];
			obj[0] = orderId;
			obj[1] = e.getKey();
			obj[2] = player.getPlayerName();
			obj[3] = e.getValue();
			kiteRanking.add(obj);
			if (orderId >= 10) {
				break;
			}
		}
		lastRankTime = TimeProcesser.getUnixTime();
	}

	public List<Entry<Long, Integer>> getRankList(Map<Long, Integer> data) {
		List<Entry<Long, Integer>> list = new ArrayList<Map.Entry<Long,Integer>>(data.entrySet());
		Collections.sort(list, new Comparator<Entry<Long, Integer>>() {
			@Override
			public int compare(Entry<Long, Integer> o1, Entry<Long, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		});
		return list;
	}
	
	public boolean chkActEnd() {
		return TimeProcesser.getUnixTime() >= endTime;
	}
	
	public void commitSync() {
		HolidayCommSerialize.getInstance().commitSync();
	}
}
